package com.yunkeji.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunkeji.common.constant.CacheConstants;
import com.yunkeji.common.core.domain.PageQuery;
import com.yunkeji.common.core.page.TableDataInfo;
import com.yunkeji.common.utils.StringUtils;
import com.yunkeji.common.utils.redis.RedisUtils;
import com.yunkeji.dao.domain.SysSensitiveWord;
import com.yunkeji.dao.mapper.SysSensitiveWordMapper;
import com.yunkeji.dao.vo.SysSensitiveWordVo;
import com.yunkeji.system.domain.bo.SysSensitiveWordBo;
import com.yunkeji.system.service.ISysSensitiveWordService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 系统-帮助Service业务层处理
 *
 * @author zhang
 * @date 2024-07-10
 */
@RequiredArgsConstructor
@Service
public class SysSensitiveWordServiceImpl implements ISysSensitiveWordService {

    private final SysSensitiveWordMapper baseMapper;

    /** 查询系统-帮助 */
    @Override
    public SysSensitiveWordVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /** 查询系统-帮助列表 */
    @Override
    public TableDataInfo<SysSensitiveWordVo> queryPageList(
            SysSensitiveWordBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysSensitiveWord> lqw = buildQueryWrapper(bo);
        Page<SysSensitiveWordVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /** 查询系统-帮助列表 */
    @Override
    public List<SysSensitiveWordVo> queryList(SysSensitiveWordBo bo) {
        LambdaQueryWrapper<SysSensitiveWord> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysSensitiveWord> buildQueryWrapper(SysSensitiveWordBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysSensitiveWord> lqw = Wrappers.lambdaQuery();
        lqw.like(
                StringUtils.isNotBlank(bo.getContent()),
                SysSensitiveWord::getContent,
                bo.getContent());
        lqw.orderByDesc(SysSensitiveWord::getCreateTime);
        return lqw;
    }

    /** 新增系统-帮助 */
    @Override
    public Boolean insertByBo(SysSensitiveWordBo bo) {
        SysSensitiveWord add = BeanUtil.toBean(bo, SysSensitiveWord.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        RedisUtils.setCacheObject(CacheConstants.CACHE_UPDATE_SENSITIVE, "", Duration.ofSeconds(1));
        return flag;
    }

    /** 修改系统-帮助 */
    @Override
    public Boolean updateByBo(SysSensitiveWordBo bo) {
        SysSensitiveWord update = BeanUtil.toBean(bo, SysSensitiveWord.class);
        validEntityBeforeSave(update);
        int i = baseMapper.updateById(update);
        RedisUtils.setCacheObject(CacheConstants.CACHE_UPDATE_SENSITIVE, "", Duration.ofSeconds(1));
        return i > 0;
    }

    /** 保存前的数据校验 */
    private void validEntityBeforeSave(SysSensitiveWord entity) {
        // TODO 做一些数据校验,如唯一约束
    }

    /** 批量删除系统-帮助 */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            // TODO 做一些业务上的校验,判断是否需要校验
        }
        int i = baseMapper.deleteBatchIds(ids);
        RedisUtils.setCacheObject(CacheConstants.CACHE_UPDATE_SENSITIVE, "", Duration.ofSeconds(1));
        return i > 0;
    }
}
